package com.chinasoft.ohos.dialog;

import com.chinasoft.ohos.ResourceTable;
import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

/**
 * ToastUtil
 *
 * @since 2021-03-29
 *
 */
public class ToastUtil {

    Context context;

    private static int radius = 58;

    public ToastUtil(Context context) {
        this.context = context;
    }

    public static void toastCtx(Context ctx, String text) {
        new ToastDialog(ctx)
                .setText(text)
                .setSize(ComponentContainer.LayoutConfig.MATCH_PARENT,ComponentContainer.LayoutConfig.MATCH_CONTENT)
                .setAutoClosable(true)
                .setDuration(5000)
                .setCornerRadius(radius)
                .show();
    }

    public static void toastCtx(Context ctx, Component layout) {
        Component customToastLayout = (Component) LayoutScatter.getInstance(ctx).parse(layout.getId(), null, false);
        ToastDialog toastDialog = new ToastDialog(ctx);
        toastDialog.setComponent(customToastLayout);
        toastDialog.setCornerRadius(radius);
        toastDialog.setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        toastDialog.setAlignment(LayoutAlignment.CENTER);
        toastDialog.show();
    }

    public void toast(String text) {
        new ToastDialog(context)
                .setText(text)
                .setSize(ComponentContainer.LayoutConfig.MATCH_PARENT,ComponentContainer.LayoutConfig.MATCH_CONTENT)
                .setAutoClosable(true)
                .setDuration(5000)
                .setCornerRadius(radius)
                .show();
    }

    public void toast(String text, int duration) {
        new ToastDialog(context)
                .setText(text)
                .setSize(ComponentContainer.LayoutConfig.MATCH_PARENT,ComponentContainer.LayoutConfig.MATCH_CONTENT)
                .setDuration(duration)
                .setCornerRadius(radius)
                .show();
    }

    public void toast(String text, int duration, int offsetX, int offsetY, int gravity) {
        new ToastDialog(context)
                .setText(text)
                .setSize(ComponentContainer.LayoutConfig.MATCH_PARENT,ComponentContainer.LayoutConfig.MATCH_CONTENT)
                .setAlignment(gravity)
                .setDuration(duration)
                .setCornerRadius(radius)
                .setOffset(offsetX, offsetY)
                .show();
    }

    public static void toast(Context context, String text) {
        ComponentContainer customToastLayout = (ComponentContainer) LayoutScatter.getInstance(context).parse(ResourceTable.Layout_toast_dialog_layout, null, false);

        Text textView = (Text) customToastLayout.findComponentById(ResourceTable.Id_toast_text);

        textView.setText(text);
        ToastDialog toastDialog = new ToastDialog(context);
        toastDialog.setComponent(customToastLayout);
        toastDialog.setCornerRadius(radius);
        toastDialog.setDuration(2000);
        toastDialog.setSize(ComponentContainer.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        toastDialog.setAlignment(LayoutAlignment.BOTTOM);
        toastDialog.show();
    }
}
